﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace DemoGame1
{
    public class Playscreen : Screen
    {
        public static int FINAL_STAGE = 3;
        public static int CHARACTER = 0;

        private int _stage;

        public int Stage
        {
            get { return _stage; }
            set { _stage = value; }
        }
        public Player player;
        Guard[] guards;
        Boss boss;
        int nguards;
        //Map map;
        MapWithCells map;
        //Map map;
        My2DSprite finalbackground;
        public int range = 300;
        public float SPEED = 0f;
        public int skilling = -1;
        public int width = 1000;
        public int height = 650;
        private myGame mygame;
        private Food food;
        private int showlogo = 0;
        private int gameover = 0;
        public int win = 0;
        //
        public Playscreen(myGame _myGame, int stage, string charactername)
        {
            _stage = stage;
            showlogo = 0;

            mygame = _myGame;
            player = new Player(_myGame.Content, charactername, Vector2.Zero, new Vector2(62, 102));
            food = new Food(_myGame.Content, "Food\\ga_",1, Vector2.Zero, new Vector2(104, 60));
            SPEED = player.SPEED;
            if (stage == 1)
            {

                //map = new Map(_g.Content, "backgroundgame", new Vector2(0, 0), new Vector2(2048, 1200));
                player = new Player(_myGame.Content, charactername, new Vector2(62, 250), new Vector2(62, 102));
                map = new MapWithCells(_myGame.Content, XMLDataProvider.MAP.strStage1);
                nguards = 5;

                guards = new Guard[nguards];
                guards[0] = new Guard(_myGame.Content, 1, new Vector2(730, 250), new Vector2(62, 102), this);
                guards[1] = new Guard(_myGame.Content, 1, new Vector2(1989, 370), new Vector2(62, 102), this);
                guards[2] = new Guard(_myGame.Content, 1, new Vector2(5300, 300), new Vector2(62, 102), this);
                guards[3] = new Guard(_myGame.Content, 1, new Vector2(5500, 350), new Vector2(62, 102), this);
                guards[4] = new Guard(_myGame.Content, 1, new Vector2(7000, 250), new Vector2(62, 102), this);
                food.TopLeft = new Vector2(4800, 360);
                myGame.mediaControler.LoadMusicTheme(1);
            }
            if (stage == 2)
            {

                //map = new Map(_g.Content, "backgroundgame", new Vector2(0, 0), new Vector2(2048, 1200));
                player = new Player(_myGame.Content, charactername, new Vector2(62, 250), new Vector2(62, 102));
                map = new MapWithCells(_myGame.Content, XMLDataProvider.MAP.strStage2);
                nguards = 5;
                guards = new Guard[nguards];
                guards[0] = new Guard(_myGame.Content, 2, new Vector2(230, 250), new Vector2(62, 102), this);
                guards[1] = new Guard(_myGame.Content, 2, new Vector2(1199, 370), new Vector2(62, 102), this);
                guards[2] = new Guard(_myGame.Content, 2, new Vector2(4200, 250), new Vector2(62, 102), this);
                guards[3] = new Guard(_myGame.Content, 2, new Vector2(4000, 370), new Vector2(62, 102), this);
                guards[4] = new Guard(_myGame.Content, 2, new Vector2(7500, 250), new Vector2(62, 102), this);
                food.TopLeft = new Vector2(3780, 440);
                myGame.mediaControler.LoadMusicTheme(2);
            }
            if (stage == 3)
            {

                //map = new Map(_g.Content, "backgroundgame", new Vector2(0, 0), new Vector2(2048, 1200));
                player = new Player(_myGame.Content, charactername, new Vector2(62, 250), new Vector2(62, 102));
                map = new MapWithCells(_myGame.Content, XMLDataProvider.MAP.strStage3);
                nguards = 5;
                guards = new Guard[nguards];
                guards[0] = new Guard(_myGame.Content, 1, new Vector2(1000, 250), new Vector2(62, 102), this);
                guards[1] = new Guard(_myGame.Content, 2, new Vector2(2000, 350), new Vector2(62, 102), this);
                guards[2] = new Guard(_myGame.Content, 1, new Vector2(3000, 250), new Vector2(62, 102), this);
                guards[3] = new Guard(_myGame.Content, 2, new Vector2(5000, 450), new Vector2(62, 102), this);
                guards[4] = new Guard(_myGame.Content, 1, new Vector2(6000, 250), new Vector2(62, 102), this);
                food.TopLeft = new Vector2(7500, 330);
                myGame.mediaControler.LoadMusicTheme(3);
            }
            if (stage == FINAL_STAGE)
            {
                boss = new Boss(_myGame.Content, new Vector2(8000, 370), new Vector2(62, 102), this);
                if (Global.Difficult == Global.DifficultLevel.NORMAL)
                {
                    boss.HP *= 2;
                }
                if (Global.Difficult == Global.DifficultLevel.HARD)
                {
                    boss.HP *= 3;
                }
            }
            finalbackground = player.finalbackground;

            for (int i = 0; i < nguards; i++)
            {
                if (Global.Difficult == Global.DifficultLevel.NORMAL)
                {
                    guards[i].HP *= 2;
                }
                if (Global.Difficult == Global.DifficultLevel.HARD)
                {
                    guards[i].HP *= 3;
                }
            }


            if (charactername == "Ryu")
            {
                CHARACTER = 0;
            }
            if (charactername == "Ken")
            {
                CHARACTER = 1;
            }

        }


        public void Showscreen(SpriteBatch spriteBatch, GameTime gameTime)
        {

            
            if (showlogo == 1 || showlogo <= 18)
            {
                Texture2D t = mygame.Content.Load<Texture2D>("Logo\\stage" + Stage.ToString());
                spriteBatch.Draw(t, new Vector2(350, 150), Color.White);
                showlogo++;
                return;

            }

            if (gameover != 0)
            {
                if (gameover <= 20)
                {
                    Texture2D t = mygame.Content.Load<Texture2D>("Logo\\gameover");
                    spriteBatch.Draw(t, new Vector2(350, 150), Color.White);
                    gameover++;
                }
                else
                {
                    myGame.currentScreen = CurrentScreen.StartMenu;
                }
                return;
            }
            if (win != 0)
            {
                if (win <= 20)
                {
                    Texture2D t = mygame.Content.Load<Texture2D>("Logo\\win");
                    spriteBatch.Draw(t, new Vector2(200, 150), Color.White);
                    win++;
                }
                else
                {
                    myGame.currentScreen = CurrentScreen.Credit;
                }
                return;
            }
            if (skilling != -1)
            {
                finalbackground.SpriteEffects = player.Sprites[0].SpriteEffects;
                finalbackground.Draw(spriteBatch, gameTime);
            }
            else
            {

                map.Draw(spriteBatch, gameTime);
                //  p2.Draw(spriteBatch, gameTime);
                player.Draw(spriteBatch, gameTime);

                for (int i = 0; i < nguards; i++)
                {
                    guards[i].Draw(spriteBatch, gameTime);
                }
                if (boss != null)

                    boss.Draw(spriteBatch, gameTime);
                map.DrawMask(spriteBatch, gameTime);
                //map.DrawCollision(spriteBatch, gameTime);
                food.Draw(spriteBatch, gameTime);
                player.characterStatusBar.Draw(spriteBatch, gameTime);
            }
        }

        private int SetView(KeyboardState newState)
        {
            int spritex = (int)player.TopLeft.X;
            int spritey = (int)player.TopLeft.Y;
            int xmin = range;
            int xmax = width - (int)player.Size.X - range;
            int ymin = range;
            int ymax = height - (int)player.Size.Y - range;


            if (spritex >= xmin && spritex <= xmax && spritey >= ymin && spritey <= ymax)
            {
                return 0;
            }

            if (player.lastDirection == Player.LEFT && spritex >= xmin)
            {
                return 0;
            }

            if (player.lastDirection == Player.RIGHT && spritex <= xmax)
            {
                return 0;
            }

            if (player.lastDirection == Player.UP && spritey >= ymin + 200)
            {
                return 0;
            }

            if (player.lastDirection == Player.DOWN && spritey <= ymax)
            {
                return 0;
            }

            return ScrollMap(newState);


        }
        public void Update(GameTime gameTime, KeyboardState newState)
        {
            food.Update(gameTime);
            if (food.Collidewith(player))
            {
                food.Visible = false;

                player.HP += 500;
                if (player.HP >= 1000)
                    player.HP = 1000;
            }
            if (showlogo == 0)
            {
                showlogo = 1;
                return;
            }
            if (player.dead == 20 && gameover == 0)
            {
                gameover = 1;

                return;
            }
            SPEED = player.SPEED;
            if (newState.IsKeyDown(Keys.Back))
            {
                myGame.currentScreen = CurrentScreen.StartMenu;
            }
            if (newState.IsKeyDown(Keys.F11))
            {
                for (int i = 0; i < nguards; i++)
                {
                    guards[i].HP = 0;
                    guards[i].Status = Character.DEAD;
                    guards[i].SetFrameSequence(Character.DEAD);
                    guards[i].dead = 1;
                }
                if (boss != null)
                {
                    boss.Status = Character.DEAD;
                    boss.SetFrameSequence(Character.DEAD);
                    boss.dead = 1;

                }

            }
            int n = player.Sprites[0].iTextures;
            if (player.ball.Visible || player.Status == Player.SKILL)
            {
                map.Visible = false;

            }
            else
            {
                map.Visible = true;
            }
            if (skilling != 5 && skilling != -1)
            {
                skilling++;
                return;
            }
            else
                skilling = -1;
            if (player.lockcontrol == false)
            {
                Keys[] PressedKeys = newState.GetPressedKeys();

                switch (PressedKeys.Length)
                {
                    case 1:
                        {
                            if (newState.IsKeyDown(Keys.Left))
                            {
                                if (player.Status != Player.WALK)
                                {
                                    player.SetFrameSequence(Player.WALK);
                                    player.Status = Player.WALK;
                                }
                                player.SetTransform(SpriteEffects.None);
                                player.adjustTopleft(player.TopLeft.X - SPEED, player.TopLeft.Y);
                                player.lastDirection = Player.LEFT;
                                if (player.CollideWithMap(map.collision) || player.TopLeft.X < 0 || SetView(newState) == 1)
                                    player.adjustTopleft(player.TopLeft.X + SPEED, player.TopLeft.Y);


                            }
                            if (newState.IsKeyDown(Keys.Right))
                            {
                                if (player.Status != Player.WALK)
                                {
                                    player.SetFrameSequence(Player.WALK);
                                    player.Status = Player.WALK;
                                }
                                player.SetTransform(SpriteEffects.FlipHorizontally);
                                player.lastDirection = Player.RIGHT;
                                player.adjustTopleft(player.TopLeft.X + SPEED, player.TopLeft.Y);
                                if (player.CollideWithMap(map.collision) || player.TopLeft.X + player.Size.X > width || SetView(newState) == 1)
                                    player.adjustTopleft(player.TopLeft.X - SPEED, player.TopLeft.Y);
                            }
                            if (newState.IsKeyDown(Keys.Down))
                            {
                                if (player.Status != Player.WALK)
                                {
                                    player.SetFrameSequence(Player.WALK);
                                    player.Status = Player.WALK;
                                }
                                player.lastDirection = Player.DOWN;
                                player.adjustTopleft(player.TopLeft.X, player.TopLeft.Y + SPEED);
                                if (player.CollideWithMap(map.collision) || player.TopLeft.Y + player.Size.Y > height || SetView(newState) == 1)
                                    player.adjustTopleft(player.TopLeft.X, player.TopLeft.Y - SPEED);

                            }
                            if (newState.IsKeyDown(Keys.Up))
                            {
                                if (player.Status != Player.WALK)
                                {
                                    player.SetFrameSequence(Player.WALK);
                                    player.Status = Player.WALK;
                                }
                                player.lastDirection = Player.UP;
                                player.adjustTopleft(player.TopLeft.X, player.TopLeft.Y - SPEED);
                                if (player.CollideWithMap(map.collision) || player.TopLeft.Y < 0 || SetView(newState) == 1)
                                    player.adjustTopleft(player.TopLeft.X, player.TopLeft.Y + SPEED);

                            }
                            if (newState.IsKeyDown(Keys.A))
                            {
                                if (player.Status != Player.ATK)
                                {
                                    player.SetFrameSequence(Player.ATK);
                                    player.Status = Player.ATK;
                                }
                                if(player.Sprites[0].iTextures%4==0)
                                    myGame.mediaControler.PlaySoundEffect(MediaControler.PLAYER.attack);
                            }
                            if (newState.IsKeyDown(Keys.S))
                            {
                                if (player.Status != Player.SKILL && player.Mana >= 45)
                                {
                                    player.SetFrameSequence(Player.SKILL);
                                    player.Mana -= 45;
                                    player.Status = Player.SKILL;
                                    //thaitujim
                                    myGame.mediaControler.PlaySoundEffect(MediaControler.PLAYER.skill);
                                    //thaitujim
                                }
                            }
                            break;
                        }
                    default:
                        if (player.Status != Player.STAND && player.Status != Player.DAMAGED)
                        {
                            player.SetFrameSequence(Player.STAND);
                            player.Status = Player.STAND;
                        }
                        break;

                }
            }


            if (player.Status == Player.SKILL && player.Sprites[0].iTextures == 0)
            {

                skilling = 1;
            }
            if ((player.Status == Player.SKILL && player.Sprites[0].iTextures <= 9) || player.Status == Player.DEAD)
            {
                mygame.TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 90);
            }
            else
            {
                mygame.TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 90);
            }
            player.Update(gameTime);
            for (int i = 0; i < nguards; i++)
            {
                guards[i].Update(gameTime);
            }
            if (boss != null)
                boss.Update(gameTime);
            if (IsClear() && Stage == FINAL_STAGE && win == 0)
            {
                win = 1;
            }

        }
        private int ScrollMap(KeyboardState newState)
        {
            Vector2 ufo = map.TopLeft;
            Vector2 origin = map.TopLeft;
            if (newState.IsKeyDown(Keys.Left))
            {
                ufo.X -= SPEED;
                if ((ufo.X) < 0)
                {
                    ufo.X += SPEED;
                    return 0;
                }
            }
            if (newState.IsKeyDown(Keys.Right))
            {
                ufo.X += SPEED;
                if ((ufo.X) > map.Size.X - width)
                {
                    ufo.X -= SPEED;
                    return 0;
                }
            }
            if (newState.IsKeyDown(Keys.Up))
            {
                ufo.Y -= 20;
                if ((ufo.Y) < 0)
                {
                    ufo.Y += SPEED;
                    return 0;
                }
            }
            if (newState.IsKeyDown(Keys.Down))
            {
                ufo.Y += 20;
                if ((ufo.Y) > map.Size.Y - height)
                {
                    ufo.Y -= SPEED;
                    return 0;
                }
            }

            map.TopLeft = ufo;
            Vector2 delta = ufo - origin;
            map.Move(delta);
            for (int i = 0; i < nguards; i++)
            {
                guards[i].TopLeft -= (map.TopLeft - origin);
            }
            if (boss != null)
            {
                boss.TopLeft -= (map.TopLeft - origin);
                boss.ball.TopLeft -= (map.TopLeft - origin);
            }
            food.TopLeft -= (map.TopLeft - origin);
            return 1;

        }

        public Boolean IsClear()
        {
            for (int i = 0; i < guards.Length; i++)
            {
                if (guards[i].Status != Guard.DEAD)
                    return false;
            }
            if (player.TopLeft.X < 750)
                return false;
            if (boss != null && boss.Status != Boss.DEAD) ;
            // return false;
            return true;

        }
    }
}
